package com.kn.knsqlitegenerator.files;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import com.kn.knsqlitegenerator.Param;

public class ReadConfigFile
{
	private static Map<String, String>	mapPackages;

	public static void run(String database)
	{
		readFromFile();
		getCurrentPackage(database);
	}

	private static void getCurrentPackage(String database)
	{
		if(mapPackages.containsKey(database)==true)
		{
			Param.setPackageName("com.kn."+mapPackages.get(database));
		}
		else
		{
			Param.setPackageName("com.kn");
		}
	}
	
	private static void readFromFile()
	{
		if (mapPackages == null)
		{
			mapPackages = new HashMap<String, String>();

			BufferedReader reader = null;
			String line;

			try
			{
				reader = new BufferedReader(new FileReader("Generator/Import/packages.txt"));

				while ((line = reader.readLine()) != null)
				{
					if (line.contains(":") == true)
					{
						line = line.trim();

						String[] config = line.split(":");

						if (config.length == 2)
						{
							mapPackages.put(config[0], config[1]);
						}
					}
				}

				reader.close();

			}
			catch (Exception e)
			{

			}
		}
	}
}
